﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// ラベルViewコントロール
    /// </summary>
    public class LabelViewControl : HeaderViewControl
    {
        /// <summary>
        /// プロパティ定数
        /// </summary>
        public new class Properties : HeaderViewControl.Properties
        {
            public const string StringFormat = "StringFormat";
            public const string HorizontalContentAlignment = "HorizontalContentAlignment";
        }

        /// <summary>
        /// 出力フォーマット
        /// </summary>
        public string StringFormat { get; set; }

        /// <summary>
        /// コンテンツの水平配置位置
        /// </summary>
        [Display(Name = "Content HAlign", Description = "Horizontal Content Alignment", GroupName = LayoutGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "HorizontalAlignmentList")]
        public LayoutHorizontalAlignment? HorizontalContentAlignment { get; set; }

        #region ShouldSerialize

        public  virtual bool ShouldSerializeHorizontalContentAlignment() { return HorizontalContentAlignment.HasValue; }

        #endregion

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public LabelViewControl()
        {
            ControlType = ViewControlType.Label;
        }

        /// <summary>
        /// Xamlを生成する
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            var list = base.CreateXaml(parent);
#if !SILVERLIGHT
            var node = parent.CreateElement("Label");
#else
            var node = parent.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", "Label");
#endif
            if (HorizontalContentAlignment.HasValue) node.SetAttribute("HorizontalContentAlignment", HorizontalContentAlignment.ToString());
            if (!string.IsNullOrEmpty(Item.BindPath))
            {
                var txt = node.AddElement("TextBlock");
                var bindData = Item.BindPath;
                if (!string.IsNullOrEmpty(StringFormat)) bindData += ", StringFormat=" + GetStringFormat(StringFormat);
                SetBindingAttribute(txt, "Text", bindData, true);
            }
            ApplyBaseProperty(node);
            list.Add(node);
            return list;
        }
    }

}
